<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #operationBeforeExtend="{ record }">
        <a-link @click="syncDevice(record.id)">
          <icon-sync/>
          同步
        </a-link>
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import {ref, reactive} from 'vue'
import userDeviceServer from '@/api/backend/deviceServer.js'
import {Message} from '@arco-design/web-vue'
import tool from '@/utils/tool.js'
import * as common from '@/utils/common.js'

const crudRef = ref()

const syncDevice = (id) => {
  userDeviceServer.syncDevice({id: id}).then(res => {
    res.success&&  Message.success('同步成功')
  })
}

const options = reactive({
  id: 'user_device_server',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: userDeviceServer.getList,
  add: {
    show: true,
    api: userDeviceServer.save,
    auth: ['deviceServer:save']
  },
  edit: {
    show: true,
    api: userDeviceServer.update,
    auth: ['deviceServer:update']
  },
  delete: {
    show: true,
    api: userDeviceServer.deletes,
    auth: ['deviceServer:delete']
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "类型",
    dataIndex: "type",
    formType: "select",
    search: true,
    dict: {
      data: [
        {
          label: "mqtt服务器",
          value: "1"
        },
        {
          label: "音柱服务器",
          value: "2"
        },
        {
          label: "流媒体服务器",
          value: "3"
        },
        {
          label: "环境监测服务器",
          value: "4"
        }
      ],
      translation: true
    }
  },
  {
    title: "状态",
    dataIndex: "status",
    formType: "select",
    search: true,
    dict: {
      data: [
        {
          label: "关闭",
          value: "0"
        },
        {
          label: "启动",
          value: "1"
        }
      ],
      translation: true
    }
  },
  {
    title: "ip",
    dataIndex: "host",
    formType: "input"
  },
  {
    title: "端口",
    dataIndex: "port",
    formType: "input"
  },
  {
    title: "账户",
    dataIndex: "username",
    formType: "input"
  },
  {
    title: "密码",
    dataIndex: "password",
    formType: "input"
  },
  {
    title: "api_key",
    dataIndex: "api_key",
    formType: "input"
  }
])
</script>
<script> export default {name: 'deviceServer'} </script>